package com.itboye.hutouben.activity.qianbao;

import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.itboye.hutouben.R;
import com.itboye.hutouben.base.BaseActivity;
import com.itboye.hutouben.base.MyApplcation;
import com.itboye.hutouben.presenter.DingDanPresenter;
import com.itboye.hutouben.presenter.UserPresenter;
import com.itboye.hutouben.util.ByAlert;
import com.itboye.hutouben.util.Const;
import com.itboye.hutouben.util.IsUtilUid;
import com.itboye.hutouben.util.SPUtils;
import com.itboye.hutouben.volley.ResultEntity;

import java.util.ArrayList;
import java.util.Observable;
import java.util.Observer;

/**
 * 添加银行卡
 */
public class AddBankCardActivity extends BaseActivity implements Observer{
    TextView add_shap_title_tv;
    ImageView close_icon;
    EditText add_bank_name,add_bank_code,add_bank_num,yinhangka_type,add_bank02_phone;
    TextView add_bank_yinhang,add_bank_ok;
    String name;
    UserPresenter userPresenter;
    DingDanPresenter dingDanPresenter;
    ArrayList<String> arrayList;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_add_bank_card);
        add_shap_title_tv.setText(R.string.bind_yinhangka);
        String phone = SPUtils.get(MyApplcation.ctx, null, Const.MOBILE, "") + "";
        add_bank02_phone.setText(phone);
        userPresenter = new UserPresenter(this);
        dingDanPresenter=new DingDanPresenter(this);

        add_bank_num.addTextChangedListener(new TextWatcher() {
            @Override
            public void onTextChanged(CharSequence s, int start, int before,
                                      int count) {
                // TODO Auto-generated method stub
                if (s.length() == 16) {
                    dingDanPresenter.cardLeiXing(s.toString());
                }
                if(s.length()<16){
                    add_bank_yinhang.setText("");
                }
            }
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count,
                                          int after) {
            }

            @Override
            public void afterTextChanged(Editable s) {
            }
        });
    }

    @Override
    protected void onInit() {

    }

    @Override
    protected void onReadIntent(Bundle bundle) {

    }

    @Override
    public void onClick(View v) {
        Intent intent=null;
        switch (v.getId()) {
            case R.id.close_icon:
                finish();
                break;
            case R.id.add_bank_ok:
                if (add_bank_name.getText().toString() .equals( "")) {
                    ByAlert.alert("请输入真实姓名");
                }else if (add_bank_num.getText().toString() .equals( "")) {
                    ByAlert.alert("请输入银行卡号");
                } else if(add_bank_num.getText().toString().length()<15){
                    ByAlert.alert("银行卡号有误");
                }else if(add_bank_code.getText().toString().length()!=18){
                    ByAlert.alert("请填写正确的身份证号");
                }else if(yinhangka_type.getText().toString().equals( "")){
                    ByAlert.alert("请输入开户行");
                }else if(add_bank_yinhang.getText().toString().equals( "")){
                    ByAlert.alert("银行卡号有误");
                }else {
                    showProgressDialog("请稍后", false);
                    userPresenter.sendVerificationCode("+86", add_bank02_phone.getText().toString(), 2 + "");
                }
                break;
            default:
                break;
        }
    }

    @Override
    public void update(Observable observable, Object data) {
        ResultEntity entity = handlerError(data);
        if (entity != null) {
            IsUtilUid.tianjia(entity);
            if (entity.getEventType() == UserPresenter.send_code_success) {
                ByAlert.alert(entity.getData());
                Intent intent = new Intent(this, AddBankCardThreeActivity.class);
                intent.putExtra("name", add_bank_num.getText().toString());
                intent.putExtra("xingming", add_bank_name.getText().toString());
                intent.putExtra("yhk_type", yinhangka_type.getText().toString());
                startActivity(intent);
                finish();

            }
            if (entity.getEventType() == UserPresenter.send_code_fail) {
                ByAlert.alert(entity.getData());
            }
            if (entity.getEventType() == DingDanPresenter.card_type_success) {
                arrayList = (ArrayList<String>) entity
                        .getData();
                add_bank_yinhang.setText(arrayList.get(0));

            }
            if (entity.getEventType() == DingDanPresenter.card_type_fail) {
                ByAlert.alert(entity.getData());
            }
        }
        try {
            closeProgressDialog();
        } catch (Exception e) {
        }
    }
}
